# Maintainer: Artem Konoplin <konoplin@gmail.com>
# Contributor: Ray Donnelly <mingwandroid@gmail.com>

_realname=mlt
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=7.12.0
pkgrel=3
pkgdesc="An open source multimedia framework (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.mltframework.org"
license=('spdx:LGPL-2.1-or-later')
_plugins_deps=("${MINGW_PACKAGE_PREFIX}-ffmpeg"
               "${MINGW_PACKAGE_PREFIX}-fftw"
               "${MINGW_PACKAGE_PREFIX}-frei0r-plugins"
               "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
               "${MINGW_PACKAGE_PREFIX}-libebur128"
               "${MINGW_PACKAGE_PREFIX}-libsamplerate"
               "${MINGW_PACKAGE_PREFIX}-libvorbis"
               "${MINGW_PACKAGE_PREFIX}-pango"
               "${MINGW_PACKAGE_PREFIX}-qt5-svg"
               $([[ ${CARCH} == i686 ]] || echo \
                 "${MINGW_PACKAGE_PREFIX}-qt6-5compat" \
                 "${MINGW_PACKAGE_PREFIX}-qt6-svg")
               "${MINGW_PACKAGE_PREFIX}-rtaudio"
               "${MINGW_PACKAGE_PREFIX}-rubberband"
               "${MINGW_PACKAGE_PREFIX}-SDL"
               "${MINGW_PACKAGE_PREFIX}-sox"
               "${MINGW_PACKAGE_PREFIX}-vid.stab")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-SDL2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-dlfcn"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${_plugins_deps[@]}")
optdepends=("${_plugins_deps[@]}")
source=(https://github.com/mltframework/mlt/releases/download/v${pkgver}/mlt-${pkgver}.tar.gz
        0001-cmake-fix-libs-install.patch
        0002-cmake-install-manpage.patch
        0003-cmake-fix-x86-detection.patch
        https://github.com/mltframework/mlt/pull/856.patch)
sha256sums=('48b385e83cbd5bf68bfc88631273868fbee36a41b3b7e2acd97f12b095b0083c'
            '3b93a3c15e56a3d80fdf68934ab2f51e73661b785e97334e7271278272937a4a'
            '88ddba2695d8c0cfb7e0b0be01a3caeb47c2ba2ea0677f489cc7dd2a7844885c'
            '500e518bdb3892dbdceab9e03e43a148b83c0000d3088f131d22b013b24fda48'
            '665445603db14d73c6501e41117d17ff3a4dca6583bbd5e5f9182fa2de2ff528')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
    0001-cmake-fix-libs-install.patch \
    0002-cmake-install-manpage.patch \
    0003-cmake-fix-x86-detection.patch \
    856.patch
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -DWINDOWS_DEPLOY=OFF \
      -DSWIG_PYTHON=ON \
      -DMOD_QT6=$([[ ${CARCH} != i686 ]] && echo ON || echo OFF) \
      "${srcdir}/${_realname}-${pkgver}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
